package com.zhizuobiao.controller;


import com.zhizuobiao.entity.News;
import com.zhizuobiao.enums.CategoryEnum;
import com.zhizuobiao.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {

    @Autowired private NewsService newsService;


    @RequestMapping("/list/{categoryCode}")
    public String findByCategory(HttpSession session,Model model, @PathVariable(name = "categoryCode",required = false)Integer categoryCode) throws  Exception{

        CategoryEnum[] categoryEnums=CategoryEnum.values();
        session.setAttribute("categoryEnums",categoryEnums);
        News news=new News();
        news.setCategory(categoryCode);
        List<News> newsList=newsService.findByCategory(news);
        model.addAttribute("newsList",newsList);
        return "news/index";
    }


    @RequestMapping("/query/{newsId}")
    public String findById(@PathVariable(name = "newsId",required = false)Integer newsId,Model model) throws Exception{

        News news=newsService.findOne(newsId);
        model.addAttribute("news",news);

        return "news/news";
    }





}
